package com.softeem.yuedong_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.yuedong_server.dto.Result;
import com.softeem.yuedong_server.entity.User;
import com.softeem.yuedong_server.mapper.UserMapper;
import com.softeem.yuedong_server.service.UserService;
import org.springframework.stereotype.Service;

import com.softeem.yuedong_server.dto.Result;
import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Result registry(User user) {
        //根据用户名查询是否已存在
        //构建查询条件构造器
        QueryWrapper<User> query = Wrappers.query(); //select * from user
        query.eq("username",user.getUsername()); //where username=#{username}
        //执行查询
        User relUser = getOne(query);
        if(Objects.nonNull(relUser)){
            //用户已存在
            return Result.fail().setMsg("用户已存在");

        }
        //todo 实现用户注册

        return save(user) ? Result.ok().setData(user).setMsg("注册成功") : Result.fail().setMsg("注册失败");
    }
}
